/*
 * @lc app=leetcode.cn id=67 lang=javascript
 *
 * [67] 二进制求和
 */

// @lc code=start
/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
var addBinary = function(a, b) {
    const maxNum = Math.max(a.length, b.length)
    let resString = ''
    let carryFlag = 0
    for (let index = 0; index < maxNum; index++) {
        let aNum = Number.parseInt(a[a.length - 1 - index] || '0')
        let bNum = Number.parseInt(b[b.length - 1 - index] || '0')
        if ( aNum + bNum + carryFlag === 3) {
            resString = '1' + resString
            carryFlag = 1
        } else if (aNum + bNum + carryFlag === 2) {
            resString = '0' + resString
            carryFlag = 1
        } else if (aNum + bNum + carryFlag === 1) {
            resString = '1' + resString
            carryFlag = 0
        }  else {
            resString = '0' + resString
            carryFlag = 0
        }
    }
    if (carryFlag === 1) {
        resString = '1' + resString
    }
    return resString
};
// @lc code=end

